package cc.vant.wechat;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.util.Objects;

import cc.vant.wechat.model.LoginSignupInHandler;
import cc.vant.wechat.model.LoginSignupOutHandler;
import cc.vant.wechat.model.MyApplication;
import cc.vant.wechat.model.packet.LoginPacket;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;


public class SplashActivity extends AppCompatActivity {
    private static final String TAG = "SplashActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        Objects.requireNonNull(getSupportActionBar()).hide();

        SharedPreferences data = getSharedPreferences("data", MODE_PRIVATE);
        String wechat_id = data.getString("wechat_id", "");
        String password = data.getString("password", "");

        MyApplication myApplication = (MyApplication) this.getApplication();

        NioEventLoopGroup group = new NioEventLoopGroup();
        ChannelFuture future;
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(new DelimiterBasedFrameDecoder(4096, Unpooled.copiedBuffer(MyApplication.DELIMITER)));
                            pipeline.addLast(new LoginSignupInHandler(SplashActivity.this));

                            pipeline.addLast(new LoginSignupOutHandler());

                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .option(ChannelOption.SO_KEEPALIVE, true);

            future = bootstrap.connect(MyApplication.SERVERIP, MyApplication.PORT).sync();

            Channel channel = future.channel();
            LoginPacket loginPacket = new LoginPacket("Jack", "rose");
            ByteBuf serialize = loginPacket.serialize();
            channel.writeAndFlush(Unpooled.copiedBuffer(serialize));
//            channel.closeFuture();
        } catch (Exception e) {
            e.printStackTrace();
            group.shutdownGracefully();
        }

        Log.d(TAG, "onCreate: 完成");
    }
}